package com.stedu.mall.adminservice.service;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.adminservice.mapper.AdminMapper;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import com.stedu.mall.common.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(String username, String password) throws SteduException {
        //根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null) {
            throw new SteduException("用户名错误，请重新登录");
        }
        if (admin.getStatus().equals(1)){
            throw new SteduException("您的账号已被禁用1.1");
        }
        //对用户输入的密码进行加密--使用MD5与盐加密
        String s = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        //对加密之后的密码与数据库中的密码进行比较
        if (!s.equals(admin.getPassword())) {
            throw new SteduException("密码错误，请重新登录");
        }
        return admin;
    }

    @Override
    public boolean insert(Admin admin) throws SteduException {
        if (adminMapper.selectByUsername(admin.getUsername()) != null) {
            throw new SteduException("用户名重复，无法添加");
        }
        admin.setSalt(UUIDUtil.get8UUID());
        admin.setPassword(SecureUtil.md5(SecureUtil.md5(admin.getPassword() + admin.getSalt())));
        admin.setCreateTime(new Date());
        admin.setStatus(0);
        return adminMapper.insert(admin) == 1;
    }

    @Override
    public boolean delete(Integer id) throws SteduException {
        if (adminMapper.selectById(id) == null) {
            throw new SteduException("没有这个用户，无法禁用");
        }
        return adminMapper.delete(id) == 1;
    }

    @Override
    public boolean update(Admin admin) throws SteduException {
        Admin oldAdmin = adminMapper.selectById(admin.getId());
        Admin newAdmin = admin;
        if (!oldAdmin.getUsername().equals(newAdmin.getUsername())) {
            if (adminMapper.selectByUsername(newAdmin.getUsername()) != null) {
                throw new SteduException("修改后的名字重复，无法修改");
            }
        }
        String s = SecureUtil.md5(SecureUtil.md5(newAdmin.getPassword() + oldAdmin.getSalt()));
        newAdmin.setPassword(s);
        return adminMapper.update(newAdmin) == 1;
    }
    @Override
    public boolean back(Integer id){
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        Admin admin1 = new Admin();
        admin1.setPassword(SecureUtil.md5(SecureUtil.md5("123" + salt)));
        admin1.setId(id);
        return adminMapper.update(admin1) == 1;
    }
    @Override
    public boolean changePwd(String oldPwd, String newPwd, Integer id) throws SteduException {
        /*
         * 判断输入的原密码是否正确
         * 1.根据id查询用户信息，获取原密码和盐
         * 2.把输入的原密码进行加密
         * 3.比较
         * */
        //根据id查询用户信息，获取原密码和盐
        Admin admin = adminMapper.selectById(id);
        //把输入的原密码进行加密
        String s = SecureUtil.md5(SecureUtil.md5(oldPwd + admin.getSalt()));
        //比较
        if (!s.equals(admin.getPassword())) {
            throw new SteduException("原密码错误，请重新输入");
        }

        //对新密码结合盐进行加密
        String s1 = SecureUtil.md5(SecureUtil.md5(newPwd + admin.getSalt()));
        if (s1.equals(admin.getPassword())) {
            throw new SteduException("两次密码一致，请重新输入");
        } else {
            //修改密码
            admin.setPassword(s1);
        }

        return adminMapper.update(admin) == 1;
    }

    @Override
    public Object selectByCondition(Admin condition, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//不分页
            return adminMapper.selectByCondition(condition);
        } else {//分页
            //设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            //查询
            List<Admin> admins = adminMapper.selectByCondition(condition);
            //创建分页信息的对象
            PageInfo<Admin> pageInfo = new PageInfo<>(admins);
            return pageInfo;
        }
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public List<Map<String, Object>> selectTop5Dept() {
        return adminMapper.selectTop5Dept();
    }

    @Override
    public List<Map<String, Object>> selectUserOrder() {
        return adminMapper.selectUserOrder();
    }
}
